package com.ruoyi.bookkeeping.service.impl;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.bookkeeping.domain.BookFriends;
import com.ruoyi.bookkeeping.domain.BookSort;
import com.ruoyi.bookkeeping.domain.param.AddBookDynamicParam;
import com.ruoyi.bookkeeping.domain.param.UpdateBookDynamicParam;
import com.ruoyi.bookkeeping.domain.vo.BookDynamicVo;
import com.ruoyi.bookkeeping.mapper.BookDynamicReplyMapper;
import com.ruoyi.bookkeeping.mapper.BookFriendsMapper;
import com.ruoyi.bookkeeping.mapper.BookSortMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bookkeeping.mapper.BookDynamicMapper;
import com.ruoyi.bookkeeping.domain.BookDynamic;
import com.ruoyi.bookkeeping.service.IBookDynamicService;

/**
 * 动态Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class BookDynamicServiceImpl extends ServiceImpl<BookDynamicMapper, BookDynamic> implements IBookDynamicService
{
    @Autowired
    private BookDynamicMapper bookDynamicMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BookFriendsMapper bookFriendsMapper;

    @Autowired
    private BookDynamicReplyServiceImpl bookDynamicReplyService;

    /**
     * 查询动态
     *
     * @param dynamicId 动态主键
     * @return 动态
     */
    @Override
    public BookDynamic selectBookDynamicByDynamicId(Long dynamicId)
    {
        return bookDynamicMapper.selectBookDynamicByDynamicId(dynamicId);
    }

    /**
     * 查询动态列表
     * @param userId
     * @return
     */
    @Override
    public List<BookDynamicVo> selectBookDynamicList(Long userId)
    {
        //好友动态
        MPJLambdaWrapper<BookDynamic> bookDynamicMPJLambdaWrapper = new MPJLambdaWrapper<>();
        bookDynamicMPJLambdaWrapper.select(BookDynamic::getDynamicId,BookDynamic::getCreateTime,BookDynamic::getDynamicUserId,
                BookDynamic::getDynamicGoodNum,BookDynamic::getDynamicImg,BookDynamic::getDynamicContent)
                .leftJoin(BookFriends.class,BookFriends::getFriendsId,BookDynamic::getDynamicUserId)
                .select(BookFriends::getFriendsRemark)
                .eq(BookFriends::getUserId,userId)
                .leftJoin(SysUser.class,SysUser::getUserId,BookFriends::getFriendsId)
                .select(SysUser::getAvatar);
        List<BookDynamicVo> bookDynamicVoList = baseMapper.selectJoinList(BookDynamicVo.class, bookDynamicMPJLambdaWrapper);
        //在查询自己的动态
        LambdaQueryWrapper<BookDynamic> bookDynamicLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookDynamicLambdaQueryWrapper.eq(BookDynamic::getDynamicUserId,userId);
        List<BookDynamic> bookDynamics = baseMapper.selectList(bookDynamicLambdaQueryWrapper);
        for(BookDynamic b:bookDynamics){
            BookDynamicVo bookDynamicVo = new BookDynamicVo();
            BeanUtils.copyProperties(b,bookDynamicVo);
            bookDynamicVo.setAvatar(sysUserMapper.selectUserById(userId).getAvatar());
            bookDynamicVoList.add(bookDynamicVo);
        }
        for(BookDynamicVo bdv:bookDynamicVoList){
            if(ObjectUtil.equal(bdv.getDynamicUserId(),userId)){
                bdv.setIsMyDynamic(1);
            }else{
                bdv.setIsMyDynamic(0);
            }
            bdv.setBookDynamicReplyVos(bookDynamicReplyService.selectBookDynamicReplyVoList(bdv.getDynamicId(),userId));
            bdv.getBookDynamicReplyVos().sort((t1, t2) -> t2.getCreateTime().compareTo(t1.getCreateTime()));
        }
        //根据时间排序
        bookDynamicVoList.sort((t1, t2) -> t2.getCreateTime().compareTo(t1.getCreateTime()));
        return bookDynamicVoList;
    }

    /**
     * 新增动态
     * @param addBookDynamicParam
     * @param userId
     * @return
     */
    @Override
    public int insertBookDynamic(AddBookDynamicParam addBookDynamicParam,Long userId)
    {
        BookDynamic bookDynamic = new BookDynamic();
        BeanUtils.copyProperties(addBookDynamicParam,bookDynamic);
        bookDynamic.setDynamicUserId(userId);
        bookDynamic.setCreateBy(sysUserMapper.selectUserById(userId).getNickName());
        bookDynamic.setCreateTime(DateUtils.getNowDate());
        bookDynamic.setDynamicGoodNum(0L);
        return baseMapper.insert(bookDynamic);
    }

    /**
     * 修改动态
     * @param updateBookDynamicParam
     * @param userId
     * @return
     */
    @Override
    public int updateBookDynamic(UpdateBookDynamicParam updateBookDynamicParam,Long userId)
    {
        LambdaQueryWrapper<BookDynamic> bookDynamicLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookDynamicLambdaQueryWrapper.eq(BookDynamic::getDynamicId,updateBookDynamicParam.getDynamicId());
        BookDynamic bookDynamic = baseMapper.selectOne(bookDynamicLambdaQueryWrapper);
        if(!ObjectUtil.equal(bookDynamic.getDynamicUserId(),userId)){
            return 0;
        }
        BeanUtils.copyProperties(updateBookDynamicParam,bookDynamic);
        bookDynamic.setUpdateTime(DateUtils.getNowDate());
        bookDynamic.setCreateTime(DateUtils.getNowDate());
        bookDynamic.setUpdateBy(sysUserMapper.selectUserById(userId).getNickName());
        return baseMapper.updateById(bookDynamic);
    }


    /**
     * 删除动态信息
     * @param dynamicId
     * @param userId
     * @return
     */
    @Override
    public int deleteBookDynamicByDynamicId(Long dynamicId,Long userId)
    {
        LambdaQueryWrapper<BookDynamic> bookDynamicLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookDynamicLambdaQueryWrapper.eq(BookDynamic::getDynamicId,dynamicId);
        BookDynamic bookDynamic = baseMapper.selectOne(bookDynamicLambdaQueryWrapper);
        if(!ObjectUtil.equal(bookDynamic.getDynamicUserId(),userId)){
            return 0;
        }
        return baseMapper.deleteById(dynamicId);
    }

    @Override
    public int goodBookDynamic(Long dynamicId, Long userId,Long goodOrCancle) {
        BookDynamic bookDynamic = baseMapper.selectById(dynamicId);
        if(0 == goodOrCancle){
            bookDynamic.setDynamicGoodNum(bookDynamic.getDynamicGoodNum() + 1);
        }else {
            bookDynamic.setDynamicGoodNum(bookDynamic.getDynamicGoodNum() - 1);
        }
        return baseMapper.updateById(bookDynamic);
    }
}
